package main
{
	import battle.BattleData;
	import battle.BattleRenderer;
	import battle.BattlerPlayer;
	import battle.map.Map;
	import battle.map.Tileset;
	
	import flash.display.Bitmap;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import loader.AbstractLoader;
	
	import util.TimeMng;

	public class BattleMng
	{
		protected static var _instance:BattleMng;
		
		public var isBattling:Boolean;
		
		protected var mapCache:Dictionary = new Dictionary();
		
		public var map:Map;
		
		protected var battleRenderer:BattleRenderer;
		
		protected var gameObjects:Dictionary;
		
		protected var playerObjects:BattlerPlayer;
		
		protected var lastFrameTime:int;
		
		
		public function BattleMng()
		{
			if( _instance )
			{
				throw new Error( "ResMng it is a sigleton" );
			}
		}
		
		public function init( width:int, height:int ):void
		{
			battleRenderer = new BattleRenderer( width, height, BattleRenderer.DEFAULT_BACKGROUD, gameObjects, playerObjects );
			
		}
		
		public function start( battleData:BattleData ):void
		{
			var mapId:int = 1;
		
			if( mapCache[ mapId ] != null )
			{
				map = Map( mapCache[ mapId ] );
				
				ready();
				
			}
			else
			{
				var onMapDataLoaded:Function = function( loaderObj:AbstractLoader ):void
				{
						map = new Map();
					
						map.mapId = mapId;		
						
						map.loadMap( loaderObj.content.toString() );
				
						mapCache[ mapId ] = map;
						
						var isReady:Boolean = true;
						
						for( var i:int = 0; i < map.tilesetIDs.length; i++ )
						{
							var tilesetId:String = map.tilesetIDs[i];
							
							if( Tileset.getTilesetBitmap( tilesetId ) == null )
							{
								isReady = false;
								
								var onComplete = function( loaderObj:AbstractLoader )
									{
										var bitmap:Bitmap = Bitmap( loaderObj.content );
										
										Tileset.addTileset( tilesetId, bitmap.bitmapData ); 
										
										ready();
										
									}
									
								LoaderMng.instance().loadImage( "../asset/battle/map/tilesets/" + tilesetId + ".png", "地图原件", null, onComplete, null );  	
									
							}
								
								
						}
						
						if( isReady )
						{
							ready();
						}
						
						
					}
					
					DataMng.instance().loadMapData( mapId, onMapDataLoaded );
			}
				
		}
		
		public function ready():void
		{
			if( map != null )
			{
				for( var i:int; i < map.tilesetIDs.length; i++ )
				{
					var tilesetId:String = map.tilesetIDs[i];
					
					if( Tileset.getTilesetBitmap( tilesetId ) == null )
					{
						return;
					}
				}
				
			}
			
			battleRenderer.setup( map );
			
			isBattling = true;
		}
		
		
		public static function instance():BattleMng
		{
			if( !_instance ) _instance = new BattleMng();
			
			return _instance;
		}
		
		
		public function onEnterFrame():void
		{
			if( !isBattling ) return;	
			
			var curTime:int = TimeMng.getTime();
			
			var dt:int = lastFrameTime - curTime;
			
			handleRespone();
			
			handleCmd();
			
			draw( dt );

			lastFrameTime = curTime;
			
		}
		
		public function handleRespone():void
		{
			
		}
		
		public function handleCmd():void
		{
			
		}
		
		public function draw( dt:int ):void
		{
			battleRenderer.draw( dt );
			
			ViewMng.instance().battleView.draw( battleRenderer.canvas );
			
		}
		
		
		public function onTimer():void
		{
			return;
		}
		
		public function onMouseDown( event:MouseEvent ):void
		{
			if( !isBattling ) return;		
		}
		
		public function onMouseUp( event:MouseEvent ):void
		{
			if( !isBattling ) return;
		}
		
		public function onKeyDown( event:KeyboardEvent ):void
		{
			if( !isBattling ) return;
		}
		
		public function onKeyUp( event:KeyboardEvent ):void
		{
			if( !isBattling ) return;
		}
		
		
	}
}